app
.controller('YsModalMultyCtrl', ['$scope', '$http', 'FileUploader', YsModalRadioController]);
function YsModalRadioController($scope, $http, FileUploader){

	var gridId = $scope.gridId;
	$scope.multy.ys_multy_all = false;
	$scope.radio.doubleClick = function(data) {
		console.log('---------$scope.radio.code--------------', $scope.radio.code);
		
		$scope.radio.doSelected(data);
	}
	
	/*
	 * 全选和全不选
	 */
	$scope.multy.checkAll = function() {

		for(var i = 0; i < $scope.multy.rows.length; i++) {
			$scope.multy.rows[i]['ys_multy_checkbox'] = $scope.multy.ys_multy_all;
		}
	}
	
	/*
	 * 单击行
	 */
	$scope.multy.singleClick = function(data, index) {
		var selected = $scope.multy.rows[index]['ys_multy_checkbox'];
		if(selected) {
			$scope.multy.rows[index]['ys_multy_checkbox'] = false;
		} else {
			$scope.multy.rows[index]['ys_multy_checkbox'] = true;
		}
	}
	
	// 初始化新增上传
    var uploader = $scope.uploaderForMulty = new FileUploader({
        url: $scope.URL_UPLOAD,
        autoUpload: true,
        onBeforeUploadItem: function(item) {
    		$scope.uploaderForMulty.indexOfLoading = layer.load(1, {shade: [0.5,'#000000']});
            console.info('onBeforeUploadItem', item);
        },
        onSuccessItem: function(fileItem, response, status, headers) {
        	if (response.code == 0) {
            	var fileName = response.data.fileName;
            	var indexPeriod = fileName.indexOf('.');
        		$http.post('/api/inter/tbFileInfo/add', {}, {params: 
        			{
	        			sourceId: $scope.multy.sourceId,
	    				sourceType: $scope.multy.sourceType,
	    				sourceCode: $scope.multy.sourceCode,
						preName: indexPeriod >= 0 ? fileName.substring(0, indexPeriod) : fileName,
						sufName: indexPeriod >= 0 ? fileName.substring(indexPeriod + 1, fileName.length) : '',
						fileType: response.data.fileType,
						fileSize: response.data.fileSizeLong,
						filePath: response.data.filePath
        			}
        		})
        		.success(function (response) {
        			if(response.code == 0) {
        				$scope.multy.loadModalMulty();
        				console.log('-------$scope.multy.rows----', $scope.multy.rows);
        			}
        			layer.closeAll();
        		});
    		}
            console.info('onSuccessItem', fileItem, response, status, headers);
        },
        onCompleteAll: function() {
    		layer.closeAll();
        }
    });
    
	$scope.multy.doRemoveBatch = function(data) {
		var ids = '';
		for (var i = 0; i < $scope.multy.rows.length; i++) {
			var row = $scope.multy.rows[i];
			if (row.ys_multy_checkbox) {
				ids = ids + row.id + ',';
			}
		}
		if (ids == '') {
			layer.msg('请至少选择一条数据');
			return;
		}
		$scope.showModalConfirm({
			tip: '是否删除文件？',
			ok: function() {
        		$http.post('/api/inter/tbFileInfo/deleteBatch', {}, {params: 
        		{
        			ids: ids.substring(0, ids.length - 1),
        			sourceId: $scope.multy.sourceId,
    				sourceType: $scope.multy.sourceType,
    				sourceCode: $scope.multy.sourceCode
    			}
        		})
	    		.success(function (response) {
	    			if(response.code == 0) {
	    				layer.msg(response.msg);
	    				$scope.multy.loadModalMulty();
	    				$scope.confirm.bsObj.hide();
	    				console.log('-------$scope.multy.rows----', $scope.multy.rows);
	    				//$scope.multy.rows = response.data;
	    			}
	    			layer.closeAll();
	    		});
			}
		});
	}
    
	$scope.multy.doRemoveSingle = function(index) {
		$scope.showModalConfirm({
			tip: '是否删除文件？',
			ok: function() {
        		$http.post('/api/inter/tbFileInfo/delete', {}, {params: 
        		{
        			id: $scope.multy.rows[index].id
    			}
        		})
	    		.success(function (response) {
	    			if(response.code == 0) {
	    				layer.msg(response.msg);
	    				$scope.multy.loadModalMulty();
	    				console.log('-------$scope.multy.rows----', $scope.multy.rows);
	    			}
	    			layer.closeAll();
	    		});
			}
		});
	}
    
	$scope.multy.doDownload = function(url) {
		 window.open(url);
	}
}